package com.home.customtest;

import com.custom.action.core.JdbcDao;
import com.custom.action.core.JdbcDaoProxy;
import com.custom.action.core.JdbcOpDao;
import com.custom.action.core.JdbcTableDao;
import com.custom.action.core.override.CustomTypeMappedHandler;
import com.custom.jdbc.configuration.DbCustomStrategy;
import com.custom.jdbc.configuration.DbGlobalConfig;
import com.custom.jdbc.session.JdbcSqlSessionFactory;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Properties;

/**
 * @author XiaoBai
 * @since 2024/4/18 11:46
 */
public class BaseTestConfig {

    protected static final String LOCAL_JDBC_CONFIG_TEST = "D:\\MyTest\\jdbcConnection-test.properties";
    protected static final String LOCAL_JDBC_CONFIG_DEV = "D:\\MyTest\\jdbcConnection-dev.properties";

    protected static String getEnv(String env) {
        if (env.equalsIgnoreCase("dev")) {
            return LOCAL_JDBC_CONFIG_DEV;
        }
        return LOCAL_JDBC_CONFIG_TEST;
    }

    protected static JdbcDao initJdbcDao(String profile, int order) {
        JdbcSqlSessionFactory sqlSessionFactory = getJdbcSqlSessionFactory(profile, order);

        sqlSessionFactory.getGlobalConfig().setTypeMappedHandler(new CustomTypeMappedHandler(sqlSessionFactory.getGlobalConfig()));
        sqlSessionFactory.getDbCustomStrategy().setSqlOutPrinting(true);
        sqlSessionFactory.getDbCustomStrategy().setUnderlineToCamel(true);
        return new JdbcDaoProxy(sqlSessionFactory).createProxy();
    }

    protected static JdbcOpDao initJdbcOpDao(String profile, int order) {
        JdbcSqlSessionFactory sqlSessionFactory = getJdbcSqlSessionFactory(profile, order);

        sqlSessionFactory.getGlobalConfig().setTypeMappedHandler(new CustomTypeMappedHandler(sqlSessionFactory.getGlobalConfig()));
        sqlSessionFactory.getDbCustomStrategy().setSqlOutPrinting(true);
        sqlSessionFactory.getDbCustomStrategy().setUnderlineToCamel(true);
        return new JdbcOpDao(sqlSessionFactory);
    }

    protected static JdbcTableDao initTableDao(String profile, int order) throws IOException {
        JdbcSqlSessionFactory sqlSessionFactory = getJdbcSqlSessionFactory(profile, order);

        sqlSessionFactory.getGlobalConfig().setTypeMappedHandler(new CustomTypeMappedHandler(sqlSessionFactory.getGlobalConfig()));
        sqlSessionFactory.getDbCustomStrategy().setSqlOutPrinting(true);
        sqlSessionFactory.getDbCustomStrategy().setUnderlineToCamel(true);
        return new JdbcTableDao(sqlSessionFactory);
    }

    @NotNull
    private static JdbcSqlSessionFactory getJdbcSqlSessionFactory(String profile, int order) {
        Properties properties = new Properties();
        try {
            properties.load(Files.newInputStream(new File(getEnv(profile)).toPath()));
            return new JdbcSqlSessionFactory(properties, "custom-jdbc", order);
        }catch (IOException e){
            return null;
        }
    }

    @NotNull
    private static JdbcSqlSessionFactory getJdbcSqlSessionFactory(String profile, int order, DbGlobalConfig globalConfig) {
        Properties properties = new Properties();
        try {
            properties.load(Files.newInputStream(new File(getEnv(profile)).toPath()));
            return new JdbcSqlSessionFactory(properties, "custom-jdbc", order, globalConfig);
        }catch (IOException e){
            return null;
        }
    }


    protected final JdbcDao jdbcTestDao = new JdbcDaoProxy(getJdbcSqlSessionFactory("test", 1, getDbGlobalConfig())).createProxy();
    protected final JdbcDao jdbcDevDao = new JdbcDaoProxy(getJdbcSqlSessionFactory("dev", 1, getDbGlobalConfig())).createProxy();
    protected final JdbcOpDao jdbcOpDao = new JdbcOpDao(getJdbcSqlSessionFactory("test", 1, getDbGlobalConfig()));
    protected final JdbcDao jdbcKtDao = initJdbcDao("test", 7);



    private static DbGlobalConfig getDbGlobalConfig() {
        // 全局配置
        DbGlobalConfig globalConfig = new DbGlobalConfig();

        // 策略配置
        DbCustomStrategy dbCustomStrategy = new DbCustomStrategy();
        // sql打印开关
        dbCustomStrategy.setSqlOutPrinting(true);
        // sql打印时， true为可执行的sql(即参数?已经替换为真实的值)， 默认false
//        dbCustomStrategy.setSqlOutPrintExecute(true);
        // 是否下划线转驼峰?
        dbCustomStrategy.setUnderlineToCamel(true);

        // 逻辑删除的字段(表字段)
        dbCustomStrategy.setDbFieldDeleteLogic("state");
        // 逻辑删除的标识值
        dbCustomStrategy.setDeleteLogicValue(1);
        // 未逻辑删除的标识值
        dbCustomStrategy.setNotDeleteLogicValue(0);

        globalConfig.setStrategy(dbCustomStrategy);
        return globalConfig;
    }

}
